package com.swift.maintenance.utils;

import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;

import com.swift.base.ApplicationBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.utils.EncryptUtils;

/**
 * @ClassName: UserInfoUtil
 * @Description:TODO(管理用户信息，如判断是否登录，注销时释放用户存储数据)
 * @author: HYM
 * @date: 2017-4-6 上午9:37:06
 *
 */
public class UserInfoUtil {

	final static String TAG = UserInfoUtil.class.getName();

	/**
	 * @author: HYM
	 * @Title: isLogin
	 * @Description: TODO(判断是否登录过)
	 * @param: @return
	 * @return: boolean
	 * @throws
	 */
	public static boolean isLogin() {
		boolean isLogin = false;
		// 如果有存在token,表示之前已经登录过
		if (!TextUtils.isEmpty(ApplicationBase.LocalData.getString(
				AppConfig.ACCESS_TOKEN, ""))) {
			isLogin = true;
		}
		return isLogin;
	}

	/**
	 * @author: HYM
	 * @Title: saveUserInfo
	 * @Description: TODO(保存用户信息)
	 * @param: @param isRem
	 * @param: @param userName
	 * @param: @param userPwd
	 * @return: void
	 * @throws
	 */
	public static void saveUserInfo(Boolean isRem, String account,
									String userPwd, String accessToken, int userRole, int depart_type,
									String depart_name, String head_path, String userName,int jg_role_id,int access) {
		Editor editor = ApplicationBase.LocalData.edit();
		editor.putString(AppConfig.ACCESS_TOKEN, accessToken);
		editor.putString(AppConfig.USER_ACCOUNT, account);
		String encryptPwd = EncryptUtils.encryptDES(userPwd, account);
		editor.putString(AppConfig.USER_PWD, encryptPwd);
		editor.putString("password", userPwd);
		editor.putBoolean(AppConfig.IS_REM, isRem);
		editor.putInt(AppConfig.USER_ROLE, userRole);
		editor.putInt(AppConfig.DEPART_TYPE, depart_type);
		editor.putInt(AppConfig.SUPERVISION_ROLE, jg_role_id);
		editor.putString(AppConfig.DEPART_NAME, depart_name);
		editor.putString(AppConfig.USER_LOGO, head_path);
		editor.putString(AppConfig.USER_NAME, userName);
		editor.putInt(AppConfig.ACCESS, access);
		editor.commit();

	}

	public static void clearUserInfo() {
		// 获取本地edit
		Editor editor = ApplicationBase.LocalData.edit();
		// 取sp里面的数据
		Boolean remember = ApplicationBase.LocalData.getBoolean(
				AppConfig.IS_REM, false);
		if (remember) {
			// 之前记住密码，则只删除token
			editor.remove(AppConfig.ACCESS_TOKEN);
		} else {
			// 如果没有记住，则全部删除
			editor.remove(AppConfig.ACCESS_TOKEN);
			editor.remove(AppConfig.USER_ACCOUNT);
			editor.remove(AppConfig.USER_PWD);
			editor.remove(AppConfig.USER_ROLE);
			editor.remove(AppConfig.DEPART_TYPE);
			editor.remove(AppConfig.SUPERVISION_ROLE);
			editor.remove(AppConfig.USER_LOGO);
			editor.remove(AppConfig.USER_NAME);
			editor.remove(AppConfig.ACCESS);
		}
		// 提交执行
		editor.commit();

	}

	/**
	 * @author: HYM
	 * @Title: getCurrentUserId
	 * @Description: TODO(获取当前用户ID)
	 * @return int
	 * @date: 2017-7-7
	 */
	public static int getCurrentUserId() {
		return ApplicationBase.LocalData.getInt(AppConfig.USER_ID, 0);
	}

	/**
	 * @author: HYM
	 * @Title: getCurrentUserId
	 * @Description: TODO(获取当前用户密码)
	 * @return int
	 * @date: 2017-7-7
	 */
	public static String getCurrentUserPassword() {
		return ApplicationBase.LocalData.getString("password", "");
	}

	/**
	 * @author: HYM
	 * @Title: getCurrentUserId
	 * @Description: TODO(获取当前用户账号)
	 * @return int
	 * @date: 2017-7-7
	 */
	public static String getCurrentUserAccount() {
		return ApplicationBase.LocalData.getString(AppConfig.USER_ACCOUNT, "");
	}

	/**
	 * @author: HYM
	 * @Title: getCurrentUserId
	 * @Description: TODO(获取当前用户昵称)
	 * @return int
	 * @date: 2017-7-7
	 */
	public static String getCurrentUserName() {
		return ApplicationBase.LocalData.getString(AppConfig.USER_NAME, "");
	}

	/**
	 * @author: HYM
	 * @Title: getCurrentUserId
	 * @Description: TODO(获取当前用户头像)
	 * @return int
	 * @date: 2017-7-7
	 */
	public static String getCurrentUserLogo() {
		return ApplicationBase.LocalData.getString(AppConfig.USER_LOGO, "");
	}

	public static void setCurrentUserLogo(String user_logo) {
		Editor editor = ApplicationBase.LocalData.edit();
		editor.putString(AppConfig.USER_LOGO, user_logo);
		editor.commit();
	}

	/**
	 * @author: HYM
	 * @Title: getCurrentUserDepartType
	 * @Description: TODO(获取部门ID)
	 * @return int
	 * @date: 2017-7-18
	 */
	public static int getCurrentUserDepartId() {
		return ApplicationBase.LocalData.getInt(AppConfig.DEPART_ID, -1);
	}

	/**
	 * @author: HYM
	 * @Title: getCurrentUserDepartType
	 * @Description: TODO(获取部门类别)
	 * @return int
	 * @date: 2017-7-18
	 */
	public static int getCurrentUserDepartType() {
		return ApplicationBase.LocalData.getInt(AppConfig.DEPART_TYPE, -1);
	}


	/**
	 * @author: HYM
	 * @Title: getCurrentUserSupervisionRole
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @return int 0表示监管除外 监管角色 1大队长 2参谋 3助理
	 * @date: 2017-10-20
	 */
	public static int getCurrentUserSupervisionRole() {
		return ApplicationBase.LocalData.getInt(AppConfig.SUPERVISION_ROLE, 0);
	}


	/**
	 * @author: HYM
	 * @Title: getCurrentUserAcessRole
	 * @Description: TODO(获取调查的权限)
	 * @return int 1 主管 2 调查员
	 * @date: 2018-1-9
	 */
	public static int getCurrentUserAcessRole() {
		return ApplicationBase.LocalData.getInt(AppConfig.ACCESS, 1);
	}

	public static void setCurrentUserAcessRole(int assess) {
		Editor editor = ApplicationBase.LocalData.edit();
		editor.putInt(AppConfig.ACCESS, assess);
		editor.commit();
	}

	public static void setCurrentUserName(String name) {
		Editor editor = ApplicationBase.LocalData.edit();
		editor.putString(AppConfig.USER_NAME, name);
		editor.commit();
	}



	/**
	 * @author: HYM
	 * @Title: getCurrentUserDepartType
	 * @Description: TODO(获取当前用户的角色)
	 * @return int
	 * @date: 2017-7-18
	 */
	public static int getCurrentUserRole() {
		int role = -1;
		try {
			role = ApplicationBase.LocalData.getInt(AppConfig.USER_ROLE, -1);
		} catch (Exception e) {
			role = Integer.parseInt(ApplicationBase.LocalData.getString(
					AppConfig.USER_ROLE, "-1"));
		}
		Log.e("user_role ", role +"");
		return role;
	}

	/**
	 * @author: HYM
	 * @Title: getCurrentUserDepartType
	 * @Description: TODO(获取部门名称)
	 * @return int
	 * @date: 2017-7-18
	 */
	public static String getCurrentUserDepartName() {
		return ApplicationBase.LocalData.getString(AppConfig.DEPART_NAME, "");
	}

	/**
	 * @author: HYM
	 * @Title: getCurrentUserAccessToken
	 * @Description: TODO(获取登录的Token)
	 * @return int
	 * @date: 2017-7-28
	 */
	public static String getCurrentUserAccessToken() {
		String token = ApplicationBase.LocalData.getString(AppConfig.ACCESS_TOKEN, "");
		Log.e("token", token);
		return token;
	}

	/**
	 * @author: HYM
	 * @Title: isCurrentUser
	 * @Description: TODO(判断是否当前用户)
	 * @param userId
	 * @return Boolean
	 * @date: 2017-7-7
	 */
	public static Boolean isCurrentUser(int userId) {
		return userId == getCurrentUserId();
	}
}
